import QtQuick 2.0
import QtQuick.Controls 1.1
import QtQuick.Controls.Private 1.0

TextField {
    id: numberField
    inputMethodHints: Qt.ImhFormattedNumbersOnly
    horizontalAlignment: TextInput.AlignRight

    property alias value: validator.value
    property alias minimumValue: validator.minimumValue
    property alias maximumValue: validator.maximumValue
    property alias suffix: validator.suffix
    property alias prefix: validator.prefix
    property alias decimals: validator.decimals

    validator: SpinBoxValidator {
        id: validator
        property bool ready: false // Delay validation until all properties are ready
        onTextChanged: {
            if (ready) {
                if (validator.value != 0) {
                    numberField.text = validator.text
                } else {
                    numberField.text = ""
                }
            }
        }
        Component.onCompleted: {
            if (validator.value != 0) {
                numberField.text = validator.text
            } else {
                numberField.text = ""
            }
            ready = true
        }
    }
    onAccepted: {
        if (validator.value != 0) {
            numberField.text = validator.text
        } else {
            numberField.text = ""
        }
        selectValue()
    }
    onTextChanged: {
        text = text.replace(/\./g , Qt.locale().decimalPoint)
    }

    onActiveFocusChanged: if (activeFocus) selectValue()

    function selectValue() {
        select(prefix.length, text.length - suffix.length)
    }
}
